#!/bin/sh
#

SYNCTHING_CONFIG="/userdata/system/configs/syncthing/"
SYNCTHING_LOG="/userdata/system/logs/syncthing.log"

RETVAL=0

start() {
    SYNCTHING_SECURITY="$(/usr/bin/batocera-settings-get system.security.enabled)"
	# check if syncthing web ui should be accessible from outside (system.security.enabled=0)
    if [ "$SYNCTHING_SECURITY"  != "1" ] || [ -z $SYNCTHING_SECURITY ]
	then
		SYNCTHING_GUI="http://0.0.0.0:8384"
	else
		SYNCTHING_GUI="http://127.0.0.1:8384"
	fi

	# create config directory if not present
	if [ ! -d $SYNCTHING_CONFIG ]
	then
		mkdir /userdata/system/configs/syncthing/
	fi
	
	echo -n "Starting syncthing service: "
	start-stop-daemon -S -b -q -x /usr/bin/syncthing -- serve --no-upgrade --no-browser --logflags=2 --logfile=$SYNCTHING_LOG --home=$SYNCTHING_CONFIG --gui-address=$SYNCTHING_GUI
	RETVAL=$?
	echo "done"

	return $RETVAL
}	

stop() {
	echo -n "Shutting down syncthing service: "
	start-stop-daemon -K -q -n syncthing
	RETVAL=$?
	echo "done"

	return $RETVAL
}	

restart() {
	stop
	start
}	

reload() {
	echo -n "Reloading syncthing service: "
	kill -HUP $(pidof syncthing)
	RETVAL=$?
	echo "done"

	return $RETVAL
}	

case "$1" in
	start)
  		start
	;;
	stop)
  		stop
	;;
	restart)
  		restart
	;;
	reload)
  		reload
	;;
  	*)
		echo "Usage: $0 {start|stop|restart|reload}"
		exit 1
esac

exit $?
